package com.nmc.core.dao;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.nmc.model.Track;
import com.nmc.model.Ward;

public class TrackDAOImpl extends BaseDAO implements TrackDAO {

	public void createTrack(Track track) {
		//System.out.println("in Create track");
        this.getHibernateTemplate().saveOrUpdate(track);
				
	}

	public void updateTrack(Track track) {
		
	    //System.out.println("in Update Track");
	    //System.out.println("Track ID==>" + track.getId());
        this.getHibernateTemplate().update(track);

	}

	public void deleteTrack(Track track) {
		//System.out.println("in Delete track");
	    //System.out.println("Track ID==>" + track.getId());
        this.getHibernateTemplate().delete(track);
       
	}

	public List getTrackList() {
		return this.getHibernateTemplate().loadAll(Track.class);
	}

	public Track getTrack(int trackId) {
		return (Track)this.getHibernateTemplate().get(Track.class, trackId);
	}

	public List filterTrack(Track track) {
		
		Session session = this.getSession();
		
        Criteria c = session.createCriteria(Track.class);
		
        if(!StringUtils.isBlank(track.getSuperviserId())){
        	c.add(Restrictions.like("superviserId",track.getSuperviserId()));
        }
		if(!StringUtils.isBlank(track.getSweeperId())){
			c.add(Restrictions.like("sweeperId",track.getSweeperId()));
		}

		if(track.getZoneId()>0){
        	c.add(Restrictions.like("zoneId",track.getZoneId()));
        }
		if(track.getWardId()>0){
			c.add(Restrictions.like("wardId",track.getWardId()));
		}
		return c.list();
//		String query = new String("select * from tracks ");
//		String where = ""; 
//		if(!StringUtils.isBlank(track.getSuperviserId())){
//			where = " where superviser_id like '" + track.getSuperviserId() + "'";
//		}
//		if(!StringUtils.isBlank(track.getSweeperId())){
//			if(!StringUtils.isBlank(where)){
//				where = where + " and sweeper_id like '" + track.getSweeperId() + "'";
//			}
//			else{
//				where = " where sweeper_id like '" + track.getSweeperId() + "'";
//			}
//		}
//			
//		return session.createQuery(query + where).list();
	}

	public List getWardTrackList(int wardId) {
		Session session = this.getSession();
		
        Criteria c = session.createCriteria(Track.class);
		c.add(Restrictions.like("wardId",wardId));

		return c.list();
	}
	
	
	public List getWardTrackListByEmp(int id) {
		Session session = this.getSession();
		
        Criteria c = session.createCriteria(Track.class);
		c.add(Restrictions.like("sweeperId",id+""));

		return c.list();
	}
	
}
